
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#include "miniutil/threadpool.h"
#include "miniutil/log.h"
#include "miniutil/task.h"

#include "minitest/testcase.h"

#include <iostream>

using namespace miniutil;


int tvalue2 = 0;

class testrun : public runnable
{
public:
	void run()
	{
		thread::sleep(200);
		tvalue2 = 100;
	}
} tr;

class testrun2 : public runnable
{
public:
	std::string msg;
	testrun2( const char * m ) : msg(m)
	{}

	void run()
	{
		cout << msg << std::endl;
	}
};

void tfuncrun()
{
	thread::sleep(200);
	tvalue2 = 200;
}

class threadtest2 : public testcase
{

public: 
	threadtest2()
	{
		themain.tests.push_back( this );
	}

	virtual void test()
	{
		threadpool tp;

		cout << "starting test thread pool" << endl;

		cout << "test runTask() : ";

		tp.runTask( &tr );
		thread::sleep(500);

		if ( tvalue2 == 100 )
			cout << "\t\t\tOK"<<endl;
		else
			cout << "\t\t\tFail"<<endl;

		cout << "test runTask(func &) : ";

		tp.runFunc( tfuncrun );
		thread::sleep(500);

		if ( tvalue2 == 200 )
			cout << "\t\t\tOK"<<endl;
		else
			cout << "\t\t\tFail"<<endl;

		cout << "test miniutil::sys::task :";

		miniutil::sys::task_manager tasks;

		tasks.delay( & tr, 200 );

		tvalue2 = 0;

		miniutil::plog->enableLevel( miniutil::logger::LOG_LEVEL_DEBUG );

		tasks.dump();

		cout << "\n	test check ...";
		if ( tasks.check( &tr ) == miniutil::sys::task_manager::TYPE_DELAY )
			cout << "\t\tOK\n";
		else
			cout << "\t\tFailed\n";

		thread::sleep( 800 );


		cout << "\n	test task running ...";

		if ( tvalue2 != 0 )
			cout << "\t\tOK\n";
		else
			cout << "\t\tFailed!\n";

		cout << "\n	test cancle ...";
		tvalue2 = 0;
		tasks.delay( & tr, 200 );
		thread::sleep( 100 );
		tasks.cancel( &tr );

		if ( tvalue2 == 0 )
			cout << "\t\tOK\n";
		else
			cout << "\t\tFailed!\n";


		cout << "\n	test execute order ..." << std::endl;
		testrun2 t1("1"), t2("2"), t3("3");

		tasks.delay( &t3, 1500 );
		tasks.delay( &t2, 700 );
		tasks.delay( &t1, 100 );

		thread::sleep( 1600 );
		cout << "is the order is ok? ";


		cout << "\n	test execute loop ... " << std::endl;
		tasks.loop( &t1 , 0, 500, 1 );
		thread::sleep( 1000 );
		cout << "only one time? "<< std::endl;

		cout << "\n	test execute loop ... " << std::endl;
		tasks.loop( &t2 , 0, 800, 2 );
		thread::sleep( 2000 );
		cout << "is 2 time? "<< std::endl;

		cout << "\n	test execute loop ... " << std::endl;
		tasks.loop( &t3 , 0, 800, 3 );
		thread::sleep( 3000 );
		cout << "is 3 time? "<< std::endl;
	}
}  ;

testcase * tthreadpool1 = new threadtest2();

